package br.unifor.cct.mia.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import br.unifor.cct.mia.dataenhancement.Database;

public class ArffConversor {

	public static void convert(String arffFile, String strucFile, String dataFile) throws IOException {
		
		final File stFile = new File(strucFile);
		final File dtFile = new File(dataFile);
		
		final BufferedWriter writerSt = new BufferedWriter(new FileWriter(stFile));
		final BufferedWriter writerDt = new BufferedWriter(new FileWriter(dtFile));
		BufferedReader reader = createReader(arffFile);
		
		//Cria o arquivo de estrutura
		String line = reader.readLine();
		line = line.replaceAll("\t"," ");
		line = line.toLowerCase();
		line = line.trim();
		
		while ( !line.startsWith("@data") ) {
			line = reader.readLine();	
			line = line.replaceAll("\t"," ");
			line = line.toLowerCase();
			line = line.trim();
		}
		
		//Cria arquivo de dados
		line = reader.readLine();
		while( line != null ) {
			line = line.toLowerCase();
			line = line.replaceAll("'","");
			line = line.replaceAll(" ","");
			
			if ( !line.startsWith("%") && !line.trim().equals("") ) 
				writerDt.append(line+"\n");
			
			line = reader.readLine();			
		}
		writerDt.close();

		Database db = LoadFile.loadDatabase(dataFile);
		
		reader = createReader(arffFile);
		line = reader.readLine();
		line = line.replaceAll("\t"," ");
		line = line.toLowerCase();
		line = line.trim();
		
		int attCount = 0;
		//Cria arquivo de estrutura
		while( line != null ) {
			if ( line.startsWith("@relation") ) {
				
				line = reader.readLine();
				line = line.replaceAll("\t"," ");				
				line = line.toLowerCase();
				line = line.trim();
				
				while ( !line.startsWith("@data") ) {
					
					if ( line.trim().equals("") ) {
						line = reader.readLine();
						line = line.replaceAll("\t"," ");
						line = line.toLowerCase();
						line = line.trim();
						continue;
					}
										
					line = line.substring( line.indexOf("@attribute")+"@attribute".length() );					
					line = line.trim();
					System.out.println(line);
					
					String attName = "";
					if ( line.indexOf("'")>=0 ) {
						int pos = line.indexOf("'")+1;
						
						if ( line.indexOf("{")<pos ) {
							String[] splited = line.split(" ");
							attName = splited[0];
							
							line = line.substring( line.indexOf(attName)+attName.length() );
							line = line.trim();
						}
						else {
							attName = line.substring( pos,line.indexOf("'",pos) );
							
							line = line.substring( line.indexOf("'"+attName+"' ") );
							line = line.trim();
						}
					}
					else {
						String[] splited = line.split(" ");
						attName = splited[0];
						
						line = line.substring( line.indexOf(attName)+attName.length() );
						line = line.trim();
					}
					
					String attType = "";
					if ( line.indexOf("real")>=0 ) {
						attType = "continuous false";
						
						double min = 0;
						double max = 0;
						for ( int i=0; i<db.size(); i++) {
							String[] values = db.getValues(i);
							if ( !values[attCount].equals("?")) {
								double vl = Double.parseDouble( values[attCount] );
								if ( vl < min ) min = vl;
								if ( vl > max ) max = vl;
							}
						}
						
						writerSt.append( attName+" "+attType+" "+min+" "+max+"\n" );
					}
					if ( line.indexOf("numeric")>=0 ) {
						attType = "continuous false";
						
						double min = 0;
						double max = 0;
						for ( int i=0; i<db.size(); i++) {
							String[] values = db.getValues(i);
							if ( !values[attCount].equals("?")) {
								double vl = Double.parseDouble( values[attCount] );
								if ( vl < min ) min = vl;
								if ( vl > max ) max = vl;
							}
						}
						
						writerSt.append( attName+" "+attType+" "+min+" "+max+"\n" );
					}
					else if ( line.indexOf("integer")>=0 ) {
						attType = "continuous true";
						
						double min = 0;
						double max = 0;
						for ( int i=0; i<db.size(); i++) {
							String[] values = db.getValues(i);
							if ( !values[attCount].equals("?") ) {
								double vl = Double.parseDouble( values[attCount] );
								if ( vl < min ) min = vl;
								if ( vl > max ) max = vl;
							}
						}
						
						writerSt.append( attName+" "+attType+" "+min+" "+max+"\n" );
					}
					else if ( line.indexOf("{")>=0 ) {
						int pos = line.indexOf("{")+1;
						String attValueList = line.substring( pos, line.indexOf("}") );
						attValueList = attValueList.replaceAll("'","");
						attValueList = attValueList.replaceAll(" ","");
						String values[] = attValueList.split(",");
						
						attType = "discrete";
						for ( int i =0; i<values.length; i++ ) {
							attType += " "+values[i];
						}
						
						writerSt.append( attName+" "+attType+"\n" );
					}			
					attCount++;
					
					line = reader.readLine();
					line = line.replaceAll("\t"," ");
					line = line.toLowerCase();
					line = line.trim();
				}
				break;
			}
			line = reader.readLine();
			line = line.replaceAll("\t"," ");
			line = line.toLowerCase();
			line = line.trim();
		}
		writerSt.close();		

	}
	
	private static BufferedReader createReader(String filePath) {
		BufferedReader reader = null;
		FileInputStream fIn;
		try {
			fIn = new FileInputStream(filePath);
			reader = new BufferedReader(new InputStreamReader(fIn));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}	
		return reader;
	}
	
}
